﻿<%@ Application Language="C#" %>

<script RunAt="server">

    //protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    //{
    //    // Let's write a message to show this got fired---
    //    //Response.Write("SessionID: " + Session.SessionID.ToString() + "User key: " + (string)Session["UserId"]);
    //    if (Session["UserId"] != null) // e.g. this is after an initial logon
    //    {
    //        string sKey = (string)Session["UserId"];
    //        // Accessing the Cache Item extends the Sliding Expiration automatically
    //        string sUser = (string)HttpContext.Current.Cache[sKey];
    //    }
    //}
    string IsCXTHLive = ConfigurationManager.AppSettings["TH.IsCXLive"].ToString();
    void Application_BeginRequest(object sender, EventArgs e)
    {
        string _requestUrl = Request.Url.ToString();
        if (IsCXTHLive == "1")
        {
            if (_requestUrl.IndexOf("http://203.142.18.111") < 0)
            {
                if (_requestUrl.IndexOf("http://www.") < 0)
                {
                    Response.Redirect(_requestUrl.Replace("http://", "http://www."));
                }
            }
            else
            {
                Response.Redirect(_requestUrl.Replace("http://203.142.18.111", "http://www.gluaygluay.com"));
            }
        }

        if (CE.Global.General.isMobileBrowser())
        {
            string mobileurl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MobileURL");
            Response.Redirect(mobileurl); 
        }

        string _teststr = _requestUrl.ToLower().Substring(_requestUrl.Length - 5, 5);
        if (_teststr == ".aspx")
        {
            if (!CE.Global.General.CheckAllLowerCase(_requestUrl))
            {
                Response.Redirect(_requestUrl.ToLower());
            }
        }

        string fullOrigionalpath = Request.Url.ToString();
        if (fullOrigionalpath.EndsWith("/"))
        {
            //fullOrigionalpath = fullOrigionalpath.Substring(0, fullOrigionalpath.Length - 1);
        }
        else
        {
            string[] _arrPageList = fullOrigionalpath.Split(';');
            if (fullOrigionalpath.Contains("404") && _arrPageList.Length == 2)
            {
                Response.Redirect(_arrPageList[1].ToLower() + "/");
            }
        }

        fullOrigionalpath = fullOrigionalpath.ToLower();
        
        if (fullOrigionalpath.EndsWith("/en/"))
        {
            Context.RewritePath("/home.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/car-insurance/"))
        {
            Context.RewritePath("/car-insurance/car-details.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/aboutus/"))
        {
            Context.RewritePath("/aboutus/aboutus.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/contactus/"))
        {
            Context.RewritePath("/contactus/contactus.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/faq/"))
        {
            Context.RewritePath("/faq/faq.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/news/"))
        {
            Context.RewritePath("/news/news.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/privacy/"))
        {
            Context.RewritePath("/privacy/privacy.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/terms/"))
        {
            Context.RewritePath("/terms/terms.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/sitemap/"))
        {
            Context.RewritePath("/sitemap/sitemap.aspx?lang=en-US");
        }
        else if (fullOrigionalpath.EndsWith("/en/signup-promo/"))
        {
            Context.RewritePath("/signup-promo/promotion.aspx?lang=en-US");
        }
        else
        {
            ThunderMain.URLRewriter.Rewriter.Process();
        }
    }

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e)
    {
        CE.Database.MyDatabase _myDb = new CE.Database.MyDatabase();
        
        // Code that runs when an unhandled error occurs
        Exception objErr = Server.GetLastError().GetBaseException();
        string err;
        err = "IP : " + Request.UserHostAddress;
        err = err + System.Environment.NewLine;
        err = err + "Error in: " + Request.Url.ToString() +
                          ". Error Message:" + objErr.Message.ToString();
        
        //// An error has occured on a .Net page.
        var serverError = Server.GetLastError() as HttpException;

        if (null != serverError)
        {
            int errorCode = serverError.GetHttpCode();
            // Log the error
            _myDb.CreateErrorLog(errorCode.ToString() + "_", err);
            if (404 == errorCode)
            {
                Server.ClearError();
                Server.Transfer("404-new.aspx");
            }
            else if (403 == errorCode)
            {
                Server.ClearError();
                Server.Transfer("403-new.aspx");
            }
        }
        else
        {
            // Log the error
            _myDb.CreateErrorLog("Global_", err);
        }

        _myDb = null;
    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

</script>

